//
//  GCMLoginHttpRequst.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/3/14.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMLoginHttpRequst.h"
#import <CommonCrypto/CommonDigest.h>

@implementation GCMLoginHttpRequst


+ (NSURLSessionDataTask *)postUserInfoWithUserName:(NSString *)name AndPassWord:(NSString *)password Success:(void (^) (id userInfo , NSError *))success{
    
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    NSString *passwordString = [self getMd5_16Bit_String:password];
    parameters[@"username"] = name;
    parameters[@"password"] = passwordString;
    
    [[NSUserDefaults standardUserDefaults]setObject:passwordString forKey:@"passWord"];
//    NSLog(@"=====+  %@",parameters);
    return [[CZHAppNetAPIClient sharedClient] POST:LOGIN_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"=====  %@",dict);
        if (success) {
            success(dict,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
}

-(void)logout{
    
    [GCMUserInfo instance].areaId = nil;
    [GCMUserInfo instance].auth = nil;
    [GCMUserInfo instance].departId = nil;
    [GCMUserInfo instance].uid = nil;
    [GCMUserInfo instance].position = nil;
    [GCMUserInfo instance].realName = nil;
    [GCMUserInfo instance].glc = nil;
}



#pragma mark md5加密
+ (NSString *)md5HexDigest:(NSString*)input
{
    const char* str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, (CC_LONG)strlen(str), result);
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
    
    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02X",result[i]];
    }
    
    return ret;
}

+ (NSString *)getMd5_16Bit_String:(NSString *)srcString{
    //提取32位MD5散列的中间16位
    NSString *md5_32Bit_String=[self md5HexDigest:srcString];
    NSString *result = [[md5_32Bit_String substringToIndex:24] substringFromIndex:8];//即9～25位
    result = [result lowercaseString];
    return result;
}

@end
